﻿//***********************************************************************
// Assembly         : IGolovkine.AoA.Core
// Author           : Ilia
// Created          : 06-11-2011
//
// Last Modified By : Ilia
// Last Modified On : 06-11-2011
// Description      : 
//
// Copyright        : (c) Microsoft. All rights reserved.
//***********************************************************************
using System.IO;

namespace IGolovkine.AoA.BinarySerialization
{
    /// <summary>
    /// 
    /// </summary>
    /// <remarks></remarks>
    public class SlimBinaryWriter : ISlimBinaryWriter
    {
        /// <summary>
        /// 
        /// </summary>
        private readonly System.IO.BinaryWriter _writer;
        /// <summary>
        /// Initializes a new instance of the <see cref="SlimBinaryWriter"/> class.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <remarks></remarks>
        public SlimBinaryWriter(Stream stream)
        {
            _writer = new System.IO.BinaryWriter(stream);
        }

        /// <summary>
        /// Writes the int32.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <remarks></remarks>
        public void WriteInt32(int value)
        {
            _writer.Write(value);
        }

        /// <summary>
        /// Writes the string.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <remarks></remarks>
        public void WriteString(string value)
        {
            _writer.Write(value);
        }

        /// <summary>
        /// Writes the object.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj">The obj.</param>
        /// <remarks></remarks>
        public void WriteObject<T>(T obj) where T : ISlimBinarySerializable
        {
            obj.Serialize(this);
        }

        /// <summary>
        /// Writes the object array.
        /// </summary>
        /// <param name="array">The array.</param>
        /// <remarks></remarks>
        public void WriteObjectArray(ISlimBinarySerializable[] array)
        {
            TypeMap map = new TypeMap(array);
            map.Serialize(this);
            foreach (ISlimBinarySerializable obj in array)
            {
                this.WriteObject(obj);
            }
        }

        /// <summary>
        /// Writes the byte array.
        /// </summary>
        /// <param name="array">The array.</param>
        /// <remarks></remarks>
        public void WriteByteArray(byte[] array)
        {
            _writer.Write(array.Length);
            _writer.Write(array);
        }
    }
}
